<?php

declare(strict_types=1);

namespace App\Controller\Admin\Common;

use App\Entity\AdminModule;
use App\Entity\AdminUserGroup;
use App\Utils\Annotation\ApiName;
use App\Utils\Helper\GeneralHelper;
use App\Utils\Traits\ControllerTrait;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class UserController
 * @package App\Controller\Admin\Common
 * @Route("/admin/common/user", name="admin-common-user-")
 * @ApiName(name="用户公共控制器")
 */
class UserController extends AbstractController {
    use ControllerTrait;

    protected $event_dispatcher;
    protected $logger;

    public function __construct (EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) {
        $this->event_dispatcher = $eventDispatcher;
        $this->logger = $logger;
    }

    /**
     * @ApiName(name="获取当前登录用户信息")
     * @Route("/info", methods={"GET"}, name="info")
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Exception
     */
    public function info () {
        $result = [
            'id'                  => $this->getUser()->getId(),
            'username'            => $this->getUser()->getUsername(),
            'nickname'            => $this->getUser()->getNickname(),
            'email'               => $this->getUser()->getEmail(),
            'contact'             => $this->getUser()->getContact(),
            'avatar'              => GeneralHelper::getAvatar($this->getUser()->getAvatar()),
            'motto'               => $this->getUser()->getMotto(),
            'admin_user_group_id' => $this->getUser()->getAdminUserGroupId(),
            'permission_list'     => $this->getUser()->getPermissionList(),
        ];
        return json_success($result);
    }

    /**
     * @ApiName(name="获取系统模块")
     * @Route("/admin-module", methods={"GET"}, name="admin-module")
     * @throws \Exception
     */
    public function getAdminModule () {
        $admin_modules = $this->getDoctrine()->getRepository(AdminModule::class)->getModules([], false, false);
        return json_success($admin_modules);
    }

    /**
     * @ApiName(name="获取用户组键值对集合", description="该数据会有十分钟缓存期")
     * @Route("/admin-user-group/list", methods={"GET"}, name="admin_user_group-list")
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Exception
     */
    public function adminUserGroupList () {
        $admin_user_groups = $this->getDoctrine()->getRepository(AdminUserGroup::class)->getCommonItems();
        return json_success($admin_user_groups);
    }

    /**
     * @ApiName(name="退出")
     * @Route("/logout", methods={"POST"}, name="logout")
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Exception
     */
    public function logout () {
        return json_success();
    }

}